package com.roast.services.my;

import com.roast.util.QueryObject;
import com.roast.domain.*;

public class GetEventIMPL extends GeneralSO implements GetEvent {

    Event event;
    QueryObject queryObject;
    DomainObject fromDB;

    @Override
    public int getEvent(Event event) {
        return new GetEventIMPL().generalExecution(event);
    }

    @Override
    protected int executeSO(Object obj) {
        if (!setEvent(obj)) {
            return 10601;
        }
        setQueryObject();
        if (!readEvent()) {
            return 10602;
        }
        if (!copyObject(obj)) {
            return 10603;
        }

        return 10600;
    }

    private boolean setEvent(Object obj) {
        try {
            this.event = (Event) obj;
            return true;
        } catch (ClassCastException ex) {
            return false;
        }
    }

    private void setQueryObject() {
        queryObject = new QueryObject();
        queryObject.setDomaninObject(event);
    }

    private boolean readEvent() {
        try {
            fromDB = dbbr.readOneByID(queryObject);
            if (queryObject.getSignal() == 10020) {
                return true;
            }
        } catch (NullPointerException ex) {
        }
        return false;
    }

    private boolean copyObject(Object obj) {
        return ((Event) obj).copyObject(fromDB);
    }

    @Override
    protected boolean executeState(int signal) {
        switch (signal) {
            case 10600:
                showOperationState(signal, "_getEvent: You have successfully read event.");
                return true;
            case 10601:
                showOperationState(signal, "_getEvent:ERROR: Class cast exception.");
                return false;
            case 10602:
                showOperationState(signal, "_getEvent:ERROR: You fail to read event from database.");
                return false;
            case 10603:
                showOperationState(signal, "_getEvent:ERROR: Fail to copy object.");
                return false;
        }
        return false;
    }

}
